package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.system.domain.DingDan;
import com.ruoyi.system.domain.DingDanDetail;
import com.ruoyi.system.mapper.DingDanDetailMapper;
import com.ruoyi.system.service.IDingDanDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 订单详情信息 服务实现类
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-16
 */
@Service
public class DingDanDetailServiceImpl extends ServiceImpl<DingDanDetailMapper, DingDanDetail> implements IDingDanDetailService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveList(DingDan dingDan) {
        for (DingDanDetail detail : dingDan.getList()) {
            detail.setHuoPinId(detail.getId());
            detail.setDingDanId(dingDan.getId());
            detail.setCreateBy(dingDan.getCreateBy());
            detail.setCreateTime(dingDan.getCreateTime());

            detail.setId(null);
            baseMapper.insert(detail);
        }
        return true;
    }

    @Override
    public List<DingDanDetail> selectByDingDan(Long id) {
        return baseMapper.selectListByDangKouId(id);
    }

    @Override
    public void deleteDetail(DingDan dingDan) {
        LambdaUpdateWrapper<DingDanDetail> params = new LambdaUpdateWrapper<>();
        params.set(DingDanDetail::getDelFlag, DelFlag.DEL.getCode());

        params.eq(DingDanDetail::getDingDanId, dingDan.getId());
        baseMapper.update(params);
    }
}
